package 反射;

import java.lang.reflect.Field;

public class TestCarFanShe {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        String classAllPath="反射.Car";
        //1.获取Car类对象堆中CLass对象
        Class<?> aClass = Class.forName(classAllPath);

        System.out.println("aClass = " + aClass);
        System.out.println("aClass.getClasses() = " + aClass.getClasses());
        System.out.println("aClass.getPackage().getName() = " + aClass.getPackage().getName());
        System.out.println("aClass.getName() = " + aClass.getName());
        Car car = (Car) aClass.newInstance();
        System.out.println("car = " + car);
        //通过反射获取属性
        Field brand = aClass.getField("brand");
        System.out.println("brand.get(car) = " + brand.get(car));

        Field[] fields = aClass.getFields();
        for (Field f:fields){
            System.out.println("f.getName() = " + f.getName());
        }

        System.out.println("-----获取反射类对象------");
        Car car1 = new Car();//硬编码
        Class<? extends Car> aClass1 = car1.getClass();
        System.out.println("aClass1 = " + aClass1);
        //3.通过class属性获取
        System.out.println("Car.class = " + Car.class);
        //4.通过类加载器
        ClassLoader classLoader = car1.getClass().getClassLoader();
        Class<?> aClass2 = classLoader.loadClass(classAllPath);
        System.out.println("aClass2 = " + aClass2);
        System.out.println("aClass.hashCode() = " + aClass.hashCode());
        System.out.println("aClass1.hashCode() = " + aClass1.hashCode());
        System.out.println("aClass2.hashCode() = " + aClass2.hashCode());
        System.out.println("Car.class.hashCode() = " + Car.class.hashCode());

        Class<Integer> integerClass = int.class;
        System.out.println("char.class = " + char.class);
        System.out.println("boolean.class = " + boolean.class);
    }
}
